package vip.xiaonuo.biz.modular.detect.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.biz.modular.detect.entity.BizDetectFormParam;
import vip.xiaonuo.biz.modular.detect.entity.BizDetectImageParam;
import vip.xiaonuo.biz.modular.detect.entity.BizDetectResult;
import vip.xiaonuo.biz.modular.detect.service.BizDetectService;
import vip.xiaonuo.biz.modular.detect.service.utils.DetectWebSocket;
import vip.xiaonuo.common.pojo.CommonResult;

/**
 * @author
 * @date 2023/10/28、15:17
 */
@Api(tags = "病鸡检测控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@RequestMapping("/biz/detect/")
@Slf4j
public class BizDetectController {
    @Autowired
    private BizDetectService bizDetectService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private DetectWebSocket detectWebSocket;

    /**
     * 检测图像
     *
     * @param bizDetectImageParam 待检测图片
     * @return {@link CommonResult}
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("检测图片")
    @PostMapping("form")
    public CommonResult<BizDetectResult> detectImage(@RequestBody BizDetectImageParam bizDetectImageParam){
        log.info("检测图片：{}", bizDetectImageParam.getImageId());
        return bizDetectService.detect(bizDetectImageParam);
        //return bizDetectService.detect(bizDetectImageParam);
    }
    //@ApiOperationSupport(order = 1)
    //@ApiOperation("检测图片")
    //@PostMapping("form")
    //public CommonResult<BizDetectResult> detectImage(@RequestBody BizDetectImageParam bizDetectImageParam){
    //    log.info("检测图片：{}", bizDetectImageParam.getImageId());
    //    CompletableFuture<BizDetectResult> future = bizDetectService.detect(bizDetectImageParam);
    //    BizDetectResult bizDetectResult;
    //    try {
    //        bizDetectResult = future.get();
    //    } catch (InterruptedException | ExecutionException e) {
    //        throw new RuntimeException(e);
    //    }
    //    //将检测结果与组织结构信息发送到消息队列中
    //    String status = bizDetectResult.getStatus();
    //    BizDetectResultMessage bizDetectResultMessage = new BizDetectResultMessage(
    //            RandomUtil.randomString(8),
    //            bizDetectImageParam.getHenhouseId(),
    //            bizDetectImageParam.getBarnId(),
    //            bizDetectImageParam.getColumnId(),
    //            status);
    //    rabbitTemplate.convertAndSend("jyl.detect","info",bizDetectResultMessage);
    //    log.info("返回结果：{}", status);
    //    BizDetectFormParam bizDetectFormParam = new BizDetectFormParam();
    //    BeanUtil.copyProperties(bizDetectResultMessage,bizDetectFormParam);
    //    if (status.equals("异常")){
    //        bizDetectService.sendWarningEmail(bizDetectFormParam);
    //    }
    //    return CommonResult.data(bizDetectResult);
    //    //return bizDetectService.detect(bizDetectImageParam);
    //}

    @ApiOperationSupport(order = 2)
    @ApiOperation("更新状态")
    @PostMapping("update_state")
    public CommonResult updateState(@RequestBody BizDetectFormParam bizDetectFormParam){
        return bizDetectService.updateState(bizDetectFormParam);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("发送警告邮件")
    @PostMapping("send_warning_email")
    public CommonResult sendWarningEmail(@RequestBody BizDetectFormParam bizDetectFormParam){
        return bizDetectService.sendWarningEmail(bizDetectFormParam);
    }

    @GetMapping("test")
    public void test(){
        String path = "D:\\chicken_images\\images\\1762751317190762497.jpg";
        rabbitTemplate.convertAndSend("jyl.detect","path",path);
    }
}
